<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language

//Người tạo : Hồ Nhật Tân - Team 1
//Module 	: Account
//Chức năng	: Xem và cập nhật thông tin tài khoản
 
class AccountController extends BaseUserController{

	public function init(){
		parent::init();
		$tbSetting = new User_Model_Parameter();
		$temp = $tbSetting->selectSetting("config");
		$layout = unserialize($temp[0]["parameter_value"]);
		if($layout['config_template'] !="")
		{
			$this->_helper->layout()->setLayout($layout['config_template']);
			$_SESSION['config_template'] = $layout['config_template'];
		}
		else
		{
			$this->_helper->layout()->setLayout('layoutmain');
			$_SESSION['config_template'] = 'layoutmain';
		}
	}

//Action index thực hiện chức năng xuất thông tin tài khoản đang đăng nhập
    public function indexAction(){
    	$_SESSION['page_code'] = 'profile';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("profile");
		if($defaultModules!=null) 
		{
			foreach($defaultModules as $dmodule)
			{
				$content_show_widget [] = $dmodule["module_code"];
				$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
			}
		}
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("profile");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		}}
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}
		
		
    	//Start------Check Input Parameter
		//End--------Check Input Parameter
		
		//Start------Khai báo các parameter
		$dbUser = new User_Model_Users();
		$dateInt = new Util();
		//End--------Khai báo các parameter
		
		//Start------Lấy thông tin tài khoản đăng nhập
		/*if(isset($_SESSION['user_name'])){
			$username = $_SESSION['user_name'];
			$userLogin = $dbUser->selectUserInfo($username);
			$birthday = $dateInt->change_int_to_date($userLogin['birth_day']);
			$this->view->userLogin = $userLogin;
			$this->view->birthday = $birthday;
		}
		else{
			$this->_redirect('index/index');
		}*/
    	$session = new Zend_Session_Namespace('login.auth');
		if(isset($session->id_user))
		{
			$this->view->logged=1;
			$user_info= $dbUser->selectUserInfoByUserID($session->id_user);
			$this->view->userLogin = $user_info;
			$birthday = $dateInt->change_int_to_date($user_info['birth_day']);
			$this->view->birthday = $birthday;
			//$this->view->current_user_image = $user_info;
			/************ Start check permission *********/
			//$user = new User();
			//$user->init();
			//$hasmodify = $user->modifiy_content($xmlconfig->name);
			//$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else
		{
			$this->_redirect('index/index');
		}
		//End--------Lấy thông tin tài khoản đăng nhập
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
				
		/*========== Start Get Text ==============*/
			$this->view->text_title 		= $language->get('text_title');
			$this->view->text_pass 			= $language->get('text_pass');
			$this->view->text_historyorder 	= $language->get('text_viewhistory');
			$this->view->text_profile 		= $language->get('text_profile');
			$this->view->text_tip 			= $language->get('text_tip');
			$this->view->text_hisorder 		= $language->get('text_hisorder');
			
			$this->view->text_lguser 		= $language->get('text_lguser');
			$this->view->text_avatar		= $language->get('text_avatar');
			$this->view->text_username 		= $language->get('text_username');
			$this->view->text_email 		= $language->get('text_email');
			$this->view->text_currentpass	= $language->get('text_currentpass');
			
			$this->view->text_lgpass 		= $language->get('text_lgpass');
			$this->view->text_newpass		= $language->get('text_newpass');
			$this->view->text_confirmpass 	= $language->get('text_confirmpass');
			
			$this->view->text_lgprofile 	= $language->get('text_lgprofile');
			$this->view->text_name			= $language->get('text_name');
			$this->view->text_sex 			= $language->get('text_sex');
			$this->view->text_birthday 		= $language->get('text_birthday');
			$this->view->text_address		= $language->get('text_address');
			$this->view->text_career 		= $language->get('text_career');
			$this->view->text_phone			= $language->get('text_phone');
			$this->view->text_yahoo 		= $language->get('text_yahoo');
			$this->view->text_skype 		= $language->get('text_skype');
			
			$this->view->text_lgcaptcha 	= $language->get('text_lgcaptcha');
			$this->view->text_captcha		= $language->get('text_captcha');
			$this->view->text_inputcaptcha 	= $language->get('text_inputcaptcha');
			
			$this->view->text_submit 		= $language->get('text_submit');
			$this->view->text_cancel		= $language->get('text_cancel');
			
			$this->view->text_noinfo 		= $language->get('text_noinfo');
			$this->view->text_sex1			= $language->get('text_sex1');
			$this->view->text_sex0 			= $language->get('text_sex0');
		/*========== End Text ==============*/
	}
	
//Action changeprofile thực hiện chức năng cập nhật thông tin tài khoản
	public function changeprofileAction(){
		$_SESSION['page_code'] = 'profile';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("profile");
		if($defaultModules!=null) {
		foreach($defaultModules as $dmodule)
		{
			$content_show_widget [] = $dmodule["module_code"];
			$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
		} }
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("profile");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		}	}
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}
		
		
		//Start------Check Input Parameter
		//End--------Check Input Parameter
		
		//Start------Khai báo các parameter
		$dbUser = new User_Model_Users();
		$dateInt = new Util();
		//End--------Khai báo các parameter
		
		//Start------Lấy thông tin tài khoản đăng nhập
		/*if(isset($_SESSION['user_name'])){
			$username = $_SESSION['user_name'];
			$userLogin = $dbUser->selectUserInfo($username);
			$birthday = $userLogin['birth_day'];
			$dbirth = gmdate('d', $birthday);
			$mbirth = gmdate('m', $birthday);
			$ybirth = gmdate('Y', $birthday);
			$this->view->birthday = $birthday;
			$this->view->userLogin = $userLogin;
			$this->view->dbirthday = $dbirth;
			$this->view->mbirthday = $mbirth;
			$this->view->ybirthday = $ybirth;
		}
		else{
			$this->_redirect('index/index');
		}*/
		$session = new Zend_Session_Namespace('login.auth');
		if(isset($session->id_user))
		{
			$this->view->logged=1;
			$user_info= $dbUser->selectUserInfoByUserID($session->id_user);
			$this->view->userLogin = $user_info;
			$birthday = $user_info['birth_day'];
			$dbirth = gmdate('d', $birthday);
			$mbirth = gmdate('m', $birthday);
			$ybirth = gmdate('Y', $birthday);
			$this->view->birthday = $birthday;
			$this->view->userLogin = $user_info;
			$this->view->dbirthday = $dbirth;
			$this->view->mbirthday = $mbirth;
			$this->view->ybirthday = $ybirth;
			//$this->view->current_user_image = $user_info;
			/************ Start check permission *********/
			//$user = new User();
			//$user->init();
			//$hasmodify = $user->modifiy_content($xmlconfig->name);
			//$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else
		{
			$this->_redirect('index/index');
		}
		//End--------Lấy thông tin tài khoản đăng nhập
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
				
		/*========== Start Get Text ==============*/
			$this->view->text_title 		= $language->get('text_title');
			$this->view->text_pass 			= $language->get('text_pass');
			$this->view->text_profile 		= $language->get('text_profile');
			$this->view->text_tip 			= $language->get('text_tip');
			
			$this->view->text_lguser 		= $language->get('text_lguser');
			$this->view->text_avatar		= $language->get('text_avatar');
			$this->view->text_username 		= $language->get('text_username');
			$this->view->text_email 		= $language->get('text_email');
			$this->view->text_currentpass	= $language->get('text_currentpass');
			
			$this->view->text_lgprofile 	= $language->get('text_lgprofile');
			$this->view->text_name			= $language->get('text_name');
			$this->view->text_sex 			= $language->get('text_sex');
			$this->view->text_birthday 		= $language->get('text_birthday');
			$this->view->text_address		= $language->get('text_address');
			$this->view->text_career 		= $language->get('text_career');
			$this->view->text_phone			= $language->get('text_phone');
			$this->view->text_yahoo 		= $language->get('text_yahoo');
			$this->view->text_skype 		= $language->get('text_skype');
			
			$this->view->text_lgcaptcha 	= $language->get('text_lgcaptcha');
			$this->view->text_captcha		= $language->get('text_captcha');
			$this->view->text_inputcaptcha 	= $language->get('text_inputcaptcha');
			
			$this->view->text_submit 		= $language->get('text_submit');
			$this->view->text_cancel		= $language->get('text_cancel');
			
			$this->view->text_noinfo 		= $language->get('text_noinfo');
			$this->view->text_sex1			= $language->get('text_sex1');
			$this->view->text_sex0 			= $language->get('text_sex0');
			
			$this->view->error_pass6 		= $language->get('error_pass6');
			$this->view->error_pass7 		= $language->get('error_pass7');
			$this->view->error_pass8 		= $language->get('error_pass8');
			$this->view->error_pass9 		= $language->get('error_pass9');
			
			$this->view->error_profile1 	= $language->get('error_profile1');
			$this->view->error_profile2 	= $language->get('error_profile2');
			$this->view->error_profile3 	= $language->get('error_profile3');
			$this->view->error_profile4 	= $language->get('error_profile4');
			$this->view->error_profile5 	= $language->get('error_profile5');
			$this->view->error_profile6 	= $language->get('error_profile6');
			$this->view->error_profile7 	= $language->get('error_profile7');
			$this->view->error_profile8 	= $language->get('error_profile8');
			$this->view->error_profile9 	= $language->get('error_profile9');
			$this->view->error_profile10 	= $language->get('error_profile10');
			$this->view->error_profile11 	= $language->get('error_profile11');
			$this->view->error_profile12 	= $language->get('error_profile12');
			$this->view->error_profile13 	= $language->get('error_profile13');
		/*========== End Text ==============*/
		
		//Start------Khởi tạo mã captcha
	    $captcha = new Zend_Captcha_Image();
		$captcha	->setTimeout('300')
	                ->setWordLen('6')
	                ->setHeight('50')
	                ->setWidth('160')
	                ->setImgDir(APPLICATION_PATH . '/../public/captcha/images/')
	                ->setImgUrl('/captcha/images/')
	                ->setSuffix('.jpg')
	                ->setFont(APPLICATION_PATH . '/../public/captcha/font/jkasperville.ttf')
	                ->setFontSize('30')
	                ->setLineNoiseLevel('5')
	            	->setDotNoiseLevel('50')
	            	->setExpiration('1')
	           	 	->setGcFreq('1');
		$captcha->generate();
		$this->view->captcha = $captcha->render($this->view); 
		$this->view->captchaID = $captcha->getId();
		//End--------Khởi tạo mã captcha
		
		//Start------Đưa chuỗi captcha vào session
		$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
		$captchaSession->word = $captcha->getWord();
		//End--------Đưa chuỗi captcha vào session
		
		//Start------Đổi catcha mới
    	if(count($_GET) && isset($_GET["captcha"]))
		{
			//Start------Tạo mã captcha mới
			$captchaNew = $captcha->render($this->view); 
			$captchaID = $captcha->getId();
			//End--------Tạo mã captcha mới
			
			//Start------Đưa chuỗi captcha vào session
			$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
			$captchaSession->word = $captcha->getWord();
			//End--------Đưa chuỗi captcha vào session
			
			$this->_helper->json($captchaID);
		}
		//End--------Đổi catcha mới
		
		//Start------Xử lý đăng ký tài khoản
		if ($this->_request->isPost()){
			
			//Start------Lấy dữ liệu từ form đăng ký
	        $flag=true;
	        $password 	= $this->_request->getParam('txtPassword');
	        $email 		= $this->_request->getParam('txtEmail');
	        $fullname 	= $this->_request->getParam('txtFullName');
	        $sex 		= $this->_request->getParam('sltSex');
	        $dbirthday 	= $this->_request->getParam('dBirthday');
	        $mbirthday 	= $this->_request->getParam('mBirthday');
	        $ybirthday 	= $this->_request->getParam('yBirthday');
	        $address 	= $this->_request->getParam('txtAddress');
	        $career 	= $this->_request->getParam('txtCareer');
	        $phone 		= $this->_request->getParam('txtPhone');
	        $yahoo 		= $this->_request->getParam('txtYahoo');
	        $skype 		= $this->_request->getParam('txtSkype');
	        $txtCaptcha = $this->_request->getParam('txtCaptcha');
			$captchaID 	= $this->_request->getParam('hdnCaptcha');
			$now 		= $dateInt->get_time_now();
	        $birthday 	= $dateInt->change_date_to_int($mbirthday, $dbirthday, $ybirthday);
	        $imageName = $_FILES['avatarUpload']['name'];
	        $imageType = $_FILES['avatarUpload']['type'];
	        $imageSize = $_FILES['avatarUpload']['size'];
	        //End--------Lấy dữ liệu từ form đăng ký
	        
			//Start------Trả dữ liệu đã nhập về form
			//$this->view->username = $username;
			//$this->view->fullname = $fullname;
			//$this->view->email = $email;
			//$this->view->sex = $sex;
			//$this->view->dbirthday = $dbirthday;
			//$this->view->mbirthday = $mbirthday;
			//$this->view->ybirthday = $ybirthday;
			$this->view->address = $address;
			$this->view->career = $career;
			$this->view->phone = $phone;
			$this->view->yahoo = $yahoo;
			$this->view->skype = $skype;
			//End--------Trả dữ liệu đã nhập về form
			
	        //Start------Kiểm tra ảnh
	        if(!($imageName=="" || $imageName==null)){
		        if(substr($imageType,0,5)!='image'){
		        	$flag = false;
		        	$this->view->avatarError = $language->get('error_avatar1');
		        }
		        if($imageSize<1 || $imageSize>2097152){
		        	$flag = false;
		        	$this->view->avatarError = $language->get('error_avatar2');
		        }
	        }
	        //End--------Kiểm tra ảnh
	        
	        //Start------Lấy thông tin tài khoản từ session
			//$user_id = $_SESSION['id_user'];
			//$username = $_SESSION['user_name'];
			//End--------Lấy thông tin tài khoản từ session
	        
			//Start------Get Email
			$dataemail = $dbUser->getEmail($email);
			//End--------Get Email
			
			//Start------select UserInfo
			//$user = $dbUser->selectUserInfo($username);
			$user = $dbUser->selectUserInfoByUserID($session->id_user);
			$user_id = $session->id_user;
			//End--------select UserInfo
			
			//Start------Kiểm tra email nhập từ form
			if(isset($dataemail[0]) && $dataemail[0][user_id]!=$user_id){
				$flag = false;
				$this->view->emailError = $email;
			}
			//End--------Kiểm tra email nhập từ form
			
			//Start------Kiểm tra mật khẩu cũ
			if(md5($password) != $user['password']){
				$flag = false;
				$this->view->passwordError = true;
			}
			//End--------Kiểm tra mật khẩu cũ
			
			//Start------Lấy thông tin captcha từ session
         	$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
         	$captchaIterator = $captchaSession->getIterator();
         	$captchaWord = $captchaIterator['word'];
         	//End--------Lấy thông tin captcha từ session
         	
         	//Start------Kiểm tra captcha nhập từ form
			if(strtolower($txtCaptcha) != strtolower($captchaWord)){
				$flag = false;
				$this->view->captchaError = $txtCaptcha;
			}
			//End--------Kiểm tra captcha nhập từ form
			
			//Start------Lưu dữ liệu đăng ký vào database
			if($flag == true){
				
				//Start------upload Avatar
				if(!($imageName=="" || $imageName==null)){
					$directory = APPLICATION_PATH . '/../public/data/';
					$uploadResult = $dbUser->uploadAvatar($directory, $imageName);
					$avaLink = "/data/".$uploadResult;
					if(isset($uploadResult) && $uploadResult==-1){
						$this->view->avatarError = $language->get('error_avatar3');
						$flag = false;
					}
				}
				//End--------upload Avatar
				
				if($flag == true){
					if(isset($uploadResult)){
						$data=array(
									'full_name' 	=> $fullname,
									'sex' 			=> $sex,
									'birth_day' 	=> $birthday,
									'email' 		=> $email,
									'address' 		=> $address,
									'career' 		=> $career,
									'phone'			=> $phone,
									'avatar'		=> $avaLink,
									'yahoo'			=> $yahoo,
									'skype'			=> $skype,
									'updated_date' 	=> $now);
					}
					else
						$data=array(
									'full_name' 	=> $fullname,
									'sex' 			=> $sex,
									'birth_day' 	=> $birthday,
									'email' 		=> $email,
									'address' 		=> $address,
									'career' 		=> $career,
									'phone'			=> $phone,
									'yahoo'			=> $yahoo,
									'skype'			=> $skype,
									'updated_date' 	=> $now);
					
					//Start------Update account
					$result = $dbUser->updateAccount($user_id, $data);
					//End--------Update account
					
					//Start------Thông báo đăng ký thành công hoặc thất bại
					if($result == -1){
						$this->_redirect('account/success?errorprofile');
					}
					else{
						$this->_redirect('account/success?profile');
					}
					//End--------Thông báo đăng ký thành công hoặc thất bại
				}
			}
			//End--------Lưu dữ liệu đăng ký vào database
		}
		//End--------Xử lý đăng ký tài khoản
	}
	
//Action changepass thực hiện chức năng đổi mật khẩu tài khoản
	public function changepassAction(){
	$_SESSION['page_code'] = 'profile';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("profile");
		if($defaultModules!=null) {
		foreach($defaultModules as $dmodule)
		{
			$content_show_widget [] = $dmodule["module_code"];
			$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
		} }
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("profile");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		} }
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}
		
		
		//Start------Check Input Parameter
		//End--------Check Input Parameter
		
		//Start------Khai báo các parameter
		$dbUser = new User_Model_Users();
		$dateInt = new Util();
		//End--------Khai báo các parameter
		
		//Start------Kiểm tra có tài khoản đăng nhập chưa
		//if(!isset($_SESSION['user_name'])){
			//$this->_redirect('index/index');
		//}
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->_redirect('index/index');
		}
		//End--------Kiểm tra có tài khoản đăng nhập chưa
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
				
		/*========== Start Get Text ==============*/
			$this->view->text_title 		= $language->get('text_title');
			$this->view->text_pass 			= $language->get('text_pass');
			$this->view->text_profile 		= $language->get('text_profile');
			$this->view->text_tip 			= $language->get('text_tip');
			
			$this->view->text_lgpass 		= $language->get('text_lgpass');
			$this->view->text_newpass		= $language->get('text_newpass');
			$this->view->text_confirmpass 	= $language->get('text_confirmpass');
			$this->view->text_currentpass 	= $language->get('text_currentpass');
			
			$this->view->text_lgcaptcha 	= $language->get('text_lgcaptcha');
			$this->view->text_captcha		= $language->get('text_captcha');
			$this->view->text_inputcaptcha 	= $language->get('text_inputcaptcha');
			
			$this->view->text_submit 		= $language->get('text_submit');
			$this->view->text_cancel		= $language->get('text_cancel');
			
			$this->view->text_noinfo 		= $language->get('text_noinfo');
			$this->view->text_sex1			= $language->get('text_sex1');
			$this->view->text_sex0 			= $language->get('text_sex0');
			
			$this->view->error_pass1 		= $language->get('error_pass1');
			$this->view->error_pass2 		= $language->get('error_pass2');
			$this->view->error_pass3 		= $language->get('error_pass3');
			$this->view->error_pass4 		= $language->get('error_pass4');
			$this->view->error_pass5 		= $language->get('error_pass5');
			$this->view->error_pass6 		= $language->get('error_pass6');
			$this->view->error_pass7 		= $language->get('error_pass7');
			$this->view->error_pass8 		= $language->get('error_pass8');
		/*========== End Text ==============*/
		
		//Start------Khởi tạo mã captcha
	    $captcha = new Zend_Captcha_Image();
		$captcha	->setTimeout('300')
	                ->setWordLen('6')
	                ->setHeight('50')
	                ->setWidth('160')
	                ->setImgDir(APPLICATION_PATH . '/../public/captcha/images/')
	                ->setImgUrl('/captcha/images/')
	                ->setSuffix('.jpg')
	                ->setFont(APPLICATION_PATH . '/../public/captcha/font/jkasperville.ttf')
	                ->setFontSize('30')
	                ->setLineNoiseLevel('5')
	            	->setDotNoiseLevel('50')
	            	->setExpiration('1')
	           	 	->setGcFreq('1');
		$captcha->generate();
		$this->view->captcha = $captcha->render($this->view); 
		$this->view->captchaID = $captcha->getId();
		//End--------Khởi tạo mã captcha
		
		//Start------Đưa chuỗi captcha vào session
		$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
		$captchaSession->word = $captcha->getWord();
		//End--------Đưa chuỗi captcha vào session
		
		//Start------Đổi catcha mới
    	if(count($_GET) && isset($_GET["captcha"]))
		{
			//Start------Tạo mã captcha mới
			$captchaNew = $captcha->render($this->view); 
			$captchaID = $captcha->getId();
			//End--------Tạo mã captcha mới
			
			//Start------Đưa chuỗi captcha vào session
			$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
			$captchaSession->word = $captcha->getWord();
			//End--------Đưa chuỗi captcha vào session
			
			$this->_helper->json($captchaID);
		}
		//End--------Đổi catcha mới
		
		if ($this->_request->isPost()){
			
			$flag=true;
			
			//Start------Lấy dữ liệu từ form
			$password = $this->_request->getParam('txtPassword');
	        $new_pass = $this->_request->getParam('txtNewPassword');
	        $now = $dateInt->get_time_now();
	        $txtCaptcha = $this->_request->getParam('txtCaptcha');
			$captchaID = $this->_request->getParam('hdnCaptcha');
	        //End--------Lấy dữ liệu từ form
	        
			//Start------Lấy thông tin tài khoản từ session
			//$user_id = $_SESSION['id_user'];
			//$username = $_SESSION['user_name'];
			$user = $dbUser->selectUserInfoByUserID($session->id_user);
			$user_id = $session->id_user;
			//End--------Lấy thông tin tài khoản từ session
	        
			//Start------Lấy thông tin captcha từ session
         	$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
         	$captchaIterator = $captchaSession->getIterator();
         	$captchaWord = $captchaIterator['word'];
         	//End--------Lấy thông tin captcha từ session
         	
         	//Start------Kiểm tra captcha nhập từ form
			if(strtolower($txtCaptcha) != strtolower($captchaWord)){
				$flag = false;
				$this->view->captchaError = $txtCaptcha;
			}
			//End--------Kiểm tra captcha nhập từ form
			
			//Start------select UserInfo
			//$user = $dbUser->selectUserInfo($username);
			//End--------select UserInfo
			
			//Start------Kiểm tra mật khẩu cũ
			if(md5($password) != $user['password']){
				$flag = false;
				$this->view->passwordError = true;
			}
			//End--------Kiểm tra mật khẩu cũ
			
			$data=array(
				'password' 		=> md5($new_pass),
				'updated_date' 	=> $now
			);
			
			//Start------Lưu dữ liệu đăng ký vào database
			if($flag == true){
				
				//Start------Đổi mật khẩu
				$result = $dbUser->updateAccount($user_id, $data);
				//End--------Đổi mật khẩu
				
				//Start------Thông báo đăng ký thành công hoặc thất bại
				if($result == -1){
					$this->_redirect('account/success?errorpass');
				}
				else{
					$this->_redirect('account/success?pass');
				}
				//End--------Thông báo đăng ký thành công hoặc thất bại
			}
			//End--------Lưu dữ liệu đăng ký vào database
		}
		//End--------Xử lý đăng ký tài khoản
	}
	
//Action success có chức năng xuất thông báo thành công hoặc thất bại
	public function successAction(){
		$_SESSION['page_code'] = 'success';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("success");
		if($defaultModules!=null) {
		foreach($defaultModules as $dmodule)
		{
			$content_show_widget [] = $dmodule["module_code"];
			$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
		} }
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("success");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		}	}
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}
		
		
		//Start------Kiểm tra có tài khoản đăng nhập chưa
		//if(!isset($_SESSION['user_name'])){
			//$this->_redirect('index/index');
		//}
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->_redirect('index/index');
		}
		//End--------Kiểm tra có tài khoản đăng nhập chưa
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
				
		/*========== Start Get Text ==============*/
			$this->view->text_success1 		= $language->get('text_success1');
			$this->view->text_success2 		= $language->get('text_success2');
			$this->view->text_success3 		= $language->get('text_success3');
			$this->view->text_success4 		= $language->get('text_success4');
			$this->view->text_success5 		= $language->get('text_success5');
			$this->view->text_success6 		= $language->get('text_success6');
			$this->view->text_success7 		= $language->get('text_success7');
		/*========== End Text ==============*/
	}
	
	//Xem lịch sử đơn hàng
	public function historyorderAction()
	{
		$_SESSION['page_code'] = 'historyorder';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("historyorder");
		if($defaultModules!=null) {
		foreach($defaultModules as $dmodule)
		{
			$content_show_widget [] = $dmodule["module_code"];
			$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
		} }
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("historyorder");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		}	}
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}

		/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
			$this->view->text_historyorder 		= $language->get('text_historyorder');
			$this->view->text_no		 		= $language->get('text_no');
			$this->view->text_billcode	 		= $language->get('text_billcode');
			$this->view->text_createddate 		= $language->get('text_createddate');
			$this->view->text_productkinds 		= $language->get('text_productkinds');
			$this->view->text_totalbillprice 	= $language->get('text_totalbillprice');
			$this->view->text_status 			= $language->get('text_status');
			$this->view->text_viewdetail 		= $language->get('text_viewdetail');
			$this->view->text_nothavebill 		= $language->get('text_nothavebill');
			$this->view->text_profilelink 		= $language->get('text_profilelink');
			//$this->view->text_success7 		= $language->get('text_new');
			//$this->view->text_success7 		= $language->get('text_processing');
			//$this->view->text_success7 		= $language->get('text_finished');
			//$this->view->text_success7 		= $language->get('text_canceled');
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$tbBill=new User_Model_Bill();
		$tbBillDetail=new User_Model_BillDetail();
		$Util=new Util();
		//End--------Khai báo các parameter
		
		//Start------Kiểm tra có tài khoản đăng nhập chưa
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->_redirect('index/index');
		}
		//End--------Kiểm tra có tài khoản đăng nhập chưa
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		//Start------Lấy thông tin bill
		$userBills=$tbBill->getBillByUserId($session->id_user);
		foreach($userBills as $userBill)
		{
			$userBill["created_date"]= $Util->change_int_to_date($userBill["created_date"]);
			switch($userBill["status"])
			{
				case 0: $userBill["status"]=$language->get('text_new');break;
				case 1: $userBill["status"]=$language->get('text_processing');break;
				case 2: $userBill["status"]=$language->get('text_finished');break;
				case 3: $userBill["status"]=$language->get('text_canceled');break;
			}
			$userBillDetail=$tbBillDetail->getBillDetailByBillId($userBill["bill_id"]);
			$temp[$userBill["bill_id"]]=array(
							"bill_id"=>$userBill["bill_id"],
							"bill_code"=>$userBill["bill_code"],
							"created_date"=>$userBill["created_date"],
							"kinds_product"=>count($userBillDetail),
							"total_bill_price"=>$userBill["total_bill_price"],
							"status"=>$userBill["status"]
						);
		}
		$userBills=$temp;
		//End--------Lấy thông tin bill
		
		//Start------Truyền parameter qua view
		$this->view->userBills=$userBills;
		//End--------Truyền parameter qua view
		
	}
	
	//Xem lịch sử đơn hàng
	public function billorderdetailAction()
	{
		$_SESSION['page_code'] = 'billorderdetail';
		
		$tbSidebarModule = new User_Model_SidebarModule();
		$Modules = $tbSidebarModule->selectAllModule();
		foreach($Modules as $module)
		{
			$database_show_widget [] = $module["module_code"];
			$database_order_widget [$module["module_code"]] =  $module["sort_order"];
		}
		
		$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
		$defaultModules = $tbDefaultbarModule->selectAllModule("billorderdetail");
		if($defaultModules!=null) {
		foreach($defaultModules as $dmodule)
		{
			$content_show_widget [] = $dmodule["module_code"];
			$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
		} }
		
		$tbContentbarModule = new User_Model_ContentModuleToPage();
		// mac dinh homepage
		$ContentModules = $tbContentbarModule->selectAllModule("billorderdetail");
		if($ContentModules!=null) {
		foreach($ContentModules as $cmodule)
		{
			$content_show_widget [] = $cmodule["module_code"];
			$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
		}	}
		
		$front     = Zend_Controller_Front::getInstance();
		$widgets = $front->getPlugins();
	
		foreach($widgets as $widget)
		{
			if(count($Modules) > 0){
				if($widget->name !="" && in_array($widget->name, $database_show_widget))
				{
					$widget->enable = 1;
					$widget->order = $database_order_widget[$widget->name];
				}
			}
			if(count($content_show_widget) > 0){
				if($widget->name !="" && in_array($widget->name, $content_show_widget))
				{
						$widget->enable = 1;
						$widget->order = $content_order_widget[$widget->name];
				}	
			}	
		}

		/*========== Start Load Language ==========*/
		$tbLanguage = new Language_Model_Language();
			$language = new Language(APPLICATION_PATH . '/modules/user/languages/account');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
				
			$languages = $tbLanguage->getAllLanguages();
			$this->view->languages = $languages;
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
			$this->view->text_historyorderbilldetail 		= $language->get('text_historyorderbilldetail');
			$this->view->text_customerinfo		 			= $language->get('text_customerinfo');
			$this->view->text_billdetail			 		= $language->get('text_billdetail');
			$this->view->text_shippingdetail		 		= $language->get('text_shippingdetail');
			$this->view->text_payment		 				= $language->get('text_payment');
			$this->view->text_no					 		= $language->get('text_no');
			$this->view->text_productname	 				= $language->get('text_productname');
			$this->view->text_quantity				 		= $language->get('text_quantity');
			$this->view->text_price 						= $language->get('text_price');
			$this->view->text_totalprice			 		= $language->get('text_totalprice');
			$this->view->text_totalbillprice 				= $language->get('text_totalbillprice');
			$this->view->text_shippingmethod 				= $language->get('text_shippingmethod');
			$this->view->text_shippingcost 					= $language->get('text_shippingcost');
			$this->view->text_receiver 						= $language->get('text_receiver');
			$this->view->text_notes 						= $language->get('text_notes');
			$this->view->text_nodetailfound 				= $language->get('text_nodetailfound');
			$this->view->text_name							= $language->get('text_name');
			$this->view->text_address						= $language->get('text_address');
			$this->view->text_phone							= $language->get('text_phone');
			$this->view->text_yahoo 						= $language->get('text_yahoo');
			$this->view->text_skype 						= $language->get('text_skype');
			$this->view->text_email 						= $language->get('text_email');
			$this->view->text_noinfo 						= $language->get('text_noinfo');
			$this->view->text_profilelink 						= $language->get('text_profilelink');
			$this->view->text_historyorderlink 						= $language->get('text_historyorderlink');
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$tbThing=new User_Model_Things();
		$tbUser = new User_Model_Users();
		$tbBill=new User_Model_Bill();
		$tbBillDetail=new User_Model_BillDetail();
		$tbPayment=new User_Model_Payment();
		$tbShippingMethod=new User_Model_ShippingMethod();
		$tbShippingDetail=new User_Model_ShippingDetail();
		$Util=new Util();
		//End--------Khai báo các parameter
		
		//Start------Kiểm tra có tài khoản đăng nhập chưa
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->_redirect('index/index');
		}
		//End--------Kiểm tra có tài khoản đăng nhập chưa
		
		//Start------Xử lý thoát tài khoản
		if(isset($_GET['logout'])){
			unset($_SESSION['id_user']);
			unset($_SESSION['user_name']);
			$this->_redirect('index/');
		}
		//End--------Xử lý thoát tài khoản
		
		//Start------Kiểm tra bill id có hợp lệ ko
		if(!isset($_GET["bill"]) || !$Util->is_interger($_GET["bill"]))
		{
			$this->_redirect('index/index');
		}
		//End--------Kiểm tra bill id có hợp lệ ko
		
		//Start------Lấy thông tin bill
		$userInfo=$tbUser->selectUserInfoByUserID($session->id_user);
		$userBills=$tbBill->getBillByBillId($_GET["bill"]);
		$userBillDetails=$tbBillDetail->getBillDetailByBillId($userBills[0]["bill_id"]);
		foreach($userBillDetails as $userBillDetail)
		{
			$thing=$tbThing->getThingByIdEvenDeleted($userBillDetail["thing_id"]);
			if(count($thing)>0)
			{
				$thing_title=$thing[0]["title"];
				$show_link=true;
			}
			else
			{
				$thing_title=$language->get('text_noinfoproduct');
				$show_link=false;
			}
			$temp2[$userBillDetail["thing_id"]]=array(
						"thing_id"=>$userBillDetail["thing_id"],
						"thing_title"=>$thing_title,
						"quantity"=>$userBillDetail["quantity"],
						"price"=>$userBillDetail["price"],
						"total_price"=>$userBillDetail["total_price"],
						"show_link"=>$show_link
						);
		}
		$userBillDetails=$temp2;
		$userPayment=$tbPayment->getPaymentById($userBills[0]["payment_id"]);
		if(count($userPayment)>0)
		{
			$payment_lang=unserialize($userPayment[0]["payment_name"]);
			if(isset($payment_lang[$name['name']]))
				$userPayment[0]["payment_name"]=$payment_lang[$name['name']];
			else
				$userPayment[0]["payment_name"]=$payment_lang["Default"];
		}
		$userShippingDetail=$tbShippingDetail->getShippingDetailById($userBills[0]["shipping_detail_id"]);
		$userShippingMethod=$tbShippingMethod->getShippingMethodById($userShippingDetail[0]["shipping_id"]);
		if(count($userShippingMethod)>0)
		{
			$shipping_method_lang=unserialize($userShippingMethod[0]["shipping_name"]);
			if(isset($shipping_method_lang[$name['name']]))
				$userShippingMethod[0]["shipping_name"]=$shipping_method_lang[$name['name']];
			else
				$userShippingMethod[0]["shipping_name"]=$shipping_method_lang["Default"];
		}
		//End--------Lấy thông tin bill
		
		//Start------Truyền parameter qua view
		$this->view->userBill=$userBills[0];
		$this->view->userInfo=$userInfo;
		$this->view->userBillDetails=$userBillDetails;
		$this->view->userPayment=$userPayment[0];
		$this->view->userShippingDetail=$userShippingDetail[0];
		$this->view->userShippingMethod=$userShippingMethod[0];
		//End--------Truyền parameter qua view
	}
}